package org.sny7.openOfflineList.listener;

import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;

import org.sny7.openOfflineList.bean.Game;

public class GameListFilterModel extends AbstractListModel {
	
	private List<Game> gameList = new ArrayList<Game>();
	
	private List<Game> gameFilteredList = new ArrayList<Game>();
	
	private String title = "";

	private static final long serialVersionUID = 1L;

	public Object getElementAt(int index) {
		return gameFilteredList.get(index);
	}

	public int getSize() {
		return gameFilteredList.size();
	}
	
	public void addAll(List<Game> gameList) {
		this.gameList = gameList;
	}
	
	public void clear() {
		gameList.clear();
		gameFilteredList.clear();
		this.fireContentsChanged(this, 0, 0);
	}
	
	private void doFilter() {
		gameFilteredList.clear();
		if (this.getTitle().equals("")) {
			gameFilteredList.addAll(gameList);
		} else {
			for (Game game : gameList) {
				if (!game.getTitle().toLowerCase().contains(this.getTitle().toLowerCase())) {
					continue;
				}
				gameFilteredList.add(game);
			}
		}
		this.fireContentsChanged(this, 0, this.getSize());
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		doFilter();
	}

}
